package com.woniu.util;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Map;

/**
 * @description: 生成userId的工具类: 通过前端传递的jwt获得当前user的id
 */
@Component
public class UserContext {
    public static Long getContextEmpId() throws ParseException, ParseException {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String requestURI = request.getRequestURI();
        if (requestURI.equals("/upload/profile") || requestURI.equals("/employee/login")) {
//            return 1L;
            return 1L;
        }
        //前端传递的认证
        String jwt = request.getHeader("authentication");
        Map jwtUser = JwtUtil.getJwtUser(jwt);
        return (Long) jwtUser.get("employeeId");
//        return (Integer) jwtUser.get("employeeId");
    }
}

//
//@Component
//public class UserContext {
//    public static Long getContextEmpId() throws ParseException, ParseException {
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String requestURI = request.getRequestURI();
//        if (requestURI.equals("/upload/profile") || requestURI.equals("/employee/login")) {
//            return 1L;
//        }
//        //前端传递的认证
//        String jwt = request.getHeader("authentication");
//        Map jwtUser = JwtUtil.getJwtUser(jwt);
////        return (Long) jwtUser.get("employeeId");
//        return (Long) jwtUser.get("employeeId");
//    }
//}